package aes.client;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class BarChart {
	
	/**
	 * 
	 */
	@SuppressWarnings("unused")
	private static final long serialVersionUID = -6788144553060997476L;
	
	private ChartPanel chartPanel;
/**
 * Constructor
 * @param datasetvals
 * @param ui
 */
	public BarChart(int[] datasetvals,ClientUI ui) {
		CategoryDataset dataset = createDataset(datasetvals);
        JFreeChart chart = createChart(dataset);
        chart.setBackgroundPaint(ui.getBackground());
        chartPanel = new ChartPanel(chart);
        chartPanel.setFillZoomRectangle(true);
        chartPanel.setMouseWheelEnabled(true);
        chartPanel.setPreferredSize(new Dimension(400, 200));
    }
    
    
/**
     * Creates a sample dataset 
     */

	private   CategoryDataset createDataset(int[] data) {
		int count=0;
		for(int i=0; i<9; i++){
			count+=data[i];
		}
		// column keys...
		String val="vals";
		String[] grades = {"0-54.9","55-64","65-69","70-74","75-79","80-84","85-89","90-94","95-100"};
		// create the dataset...
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		if(count!=0){
			for(int i=0; i<9; i++){
				dataset.addValue(data[i]/count*100, val, grades[i]);
			}
		}
		else{
			for(int i=0; i<9; i++){
				dataset.addValue(data[i], val, grades[i]);
			}
		}
		return dataset; 
	}


/**
     * Creates a chart
     */

	  private JFreeChart createChart(CategoryDataset dataset) {
		  
		          // create the chart...
		          JFreeChart chart = ChartFactory.createBarChart(
		              "Grades Distribution",       // chart title
		              "Grades",               // domain axis label
		              "Precentage",                  // range axis label
		              dataset,                  // data
		              PlotOrientation.VERTICAL, // orientation
		              false,                     // include legend
		              false,                     // tooltips?
		              false                     // URLs?
		          );
		  
		          // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
		  
		          // set the background color for the chart...
		          chart.setBackgroundPaint(Color.white);
		  
		          // get a reference to the plot for further customisation...
		          CategoryPlot plot = (CategoryPlot) chart.getPlot();
	
		          // set the range axis to display integers only..
		          NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		          rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		  
		          // disable bar outlines...
		          BarRenderer renderer = (BarRenderer) plot.getRenderer();
		          renderer.setDrawBarOutline(false);
		  
		          // set up gradient paints for series...
		          GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue,
		                  0.0f, 0.0f, new Color(0, 0, 64));
		          renderer.setSeriesPaint(0, gp0);
		          CategoryAxis domainAxis = plot.getDomainAxis();
		          domainAxis.setCategoryLabelPositions(
		                  CategoryLabelPositions.createUpRotationLabelPositions(
		                          Math.PI / 6.0));
		          // OPTIONAL CUSTOMISATION COMPLETED.
		  
		          return chart;
		  
		      }
	  public ChartPanel getChartPanel(){
		  return this.chartPanel;
	  }
}
